package com.ruoyi.loan.loan.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.LoanVo;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.loan.loan.domain.TbLoanProducts;
import com.ruoyi.loan.loan.domain.TbUser;
import com.ruoyi.loan.loan.mapper.TbLoanProductsMapper;
import com.ruoyi.loan.loan.mapper.TbUserMapper;
import com.ruoyi.loan.loan.utils.LoanMoneyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.loan.loan.mapper.TbLoanMapper;
import com.ruoyi.loan.loan.domain.TbLoan;
import com.ruoyi.loan.loan.service.ITbLoanService;

/**
 * 借款Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-15
 */
@Service
public class TbLoanServiceImpl implements ITbLoanService 
{
    @Autowired
    private TbLoanMapper tbLoanMapper;

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private TbLoanProductsMapper tbLoanProductsMapper;

    /**
     * 查询借款
     * 
     * @param loanId 借款主键
     * @return 借款
     */
    @Override
    public TbLoan selectTbLoanByLoanId(Long loanId)
    {
        return tbLoanMapper.selectTbLoanByLoanId(loanId);
    }

    /**
     * 查询借款列表
     * 
     * @param tbLoan 借款
     * @return 借款
     */
    @Override
    public List<TbLoan> selectTbLoanList(TbLoan tbLoan)
    {
        return tbLoanMapper.selectTbLoanList(tbLoan);
    }

    /**
     * 新增借款
     * 
     * @param loanVo 借款
     * @return 结果
     */
    @Override
    public int insertTbLoan(LoanVo loanVo)
    {
        TbLoanProducts tbLoanProducts = tbLoanProductsMapper.selectById(loanVo.getLoanLoanProductsId());
        TbUser tbUser = tbUserMapper.selectById(loanVo.getLoanUserId());

        Double aDouble = LoanMoneyUtils.loanMoney(tbLoanProducts.getLoanProductsRates(), loanVo.getLoanLoanMoney(), loanVo.getLoanLoanLastTerm());
        loanVo.setLoanActualMoney(aDouble);
        loanVo.setCreateBy(tbUser.getUserName());
        loanVo.setUpdateBy(tbUser.getUserName());
        loanVo.setCreateTime(null);
        loanVo.setUpdateTime(null);

        TbLoan tbLoan = new TbLoan();
        BeanUtils.copyProperties(loanVo, tbLoan);
        return tbLoanMapper.insertTbLoan(tbLoan);
    }

    /**
     * 修改借款
     * 
     * @param loanVo 借款
     * @return 结果
     */
    @Override
    public int updateTbLoan(LoanVo loanVo)
    {
        TbLoanProducts tbLoanProducts = tbLoanProductsMapper.selectById(loanVo.getLoanLoanProductsId());
        Double aDouble = LoanMoneyUtils.loanMoney(tbLoanProducts.getLoanProductsRates(), loanVo.getLoanLoanMoney(), loanVo.getLoanLoanLastTerm());
        loanVo.setLoanActualMoney(aDouble);
        TbUser tbUser = tbUserMapper.selectById(loanVo.getLoanUserId());
        loanVo.setUpdateBy(tbUser.getUserName());
        loanVo.setUpdateTime(null);

        TbLoan tbLoan = new TbLoan();
        BeanUtils.copyProperties(loanVo, tbLoan);
        return tbLoanMapper.updateTbLoan(tbLoan);
    }

    /**
     * 批量删除借款
     * 
     * @param loanIds 需要删除的借款主键
     * @return 结果
     */
    @Override
    public int deleteTbLoanByLoanIds(Long[] loanIds)
    {

        return tbLoanMapper.deleteTbLoanByLoanIds(loanIds);
    }

    /**
     * 删除借款信息
     * 
     * @param loanId 借款主键
     * @return 结果
     */
    @Override
    public int deleteTbLoanByLoanId(Long loanId)
    {

        return tbLoanMapper.deleteTbLoanByLoanId(loanId);
    }
}
